(function() {
	var app, deps;

	deps = [ 'treeGrid' ];

	app = angular.module('treeGridTest', deps);

	app.controller('treeGridController', function($scope, $timeout) {
		var tree;

		var myTreeData = getTreeData();

		$scope.tree_data = myTreeData;// 赋值语句
		$scope.my_tree = tree = {};
		$scope.expanding_property = {
			field : "menuName",
			displayName : "菜单名",
			sortable : true,
			filterable : true,
			cellTemplate : "<i>{{row.branch[expandingProperty.field]}}</i>"
		};
		$scope.col_defs = [];

		$scope.my_tree_handler = function(branch) {
			//console.log('you clicked on', branch)

		}
	});

	app.controller('TableRowController', function($scope, $timeout) {
		$scope.tableRow = function(branch) {
			//记录此次单击的父id
			
			$("#parentMenuIdHidden").val(branch.id);
			$("#parentMenuLevelHidden").val(branch.menuLevel);
			$("#parentMenuName").val(branch.menuName);
			//单击父级菜单要在右边显示自己子类菜单
			var isExistLegalSubClass = false;
			
			var subMenuHtml = '';
			$(branch.children).each(function(){
				if (this.menuType == '20171026000005') {//数据接口类型
					return false;//countinue;
				}
				isExistLegalSubClass = true;
				subMenuHtml += '<tr style="cursor:move" data-index="0" id="menuId_'+this.id+'" data-uniqueid="'+this.id+'">'
							+		'<td style="">'
							+			branch.menuName
							+		'</td>'
							+		'<td style="">'
							+			this.menuName
							+		'</td>'
							+		'<td style="">'
							+			this.menuOrder
							+		'</td>'
							+  '</tr>';
			});
			if (isExistLegalSubClass == false) {
				//子类没有功能菜单类型的菜单
				var tbodyHtml = '<tr class="no-records-found">' 
					+				'<td colspan="3">'
					+					branch.menuName+'菜单没有类型是功能菜单的子菜单'
					+				'</td>'
					+			'</tr>';
				$("#sortable").html(tbodyHtml);
			} else {
				$("#sortable").html(subMenuHtml);
			}
		}
	});

}).call(this);

$(function() {
	var myTreeData = getTreeData();
	var subMenuInitHtml = '';
	$(myTreeData).each(function(){
		subMenuInitHtml += '<tr style="cursor:move" data-index="0" id="menuId_'+this.id+'" data-uniqueid="'+this.id+'">'
						+		'<td style="">'
						+			"根菜单"
						+		'</td>'
						+		'<td style="">'
						+			this.menuName
						+		'</td>'
						+		'<td style="">'
						+			this.menuOrder
						+		'</td>'
						+	'</tr>';
	});
	$("#sortable").html(subMenuInitHtml);
	$("#sortable").sortable();
	$("#sortable").disableSelection();
});

function getTreeData() {
	var rawTreeData = '';
	$.ajax({
		url : "../menu/query.action",
		type : "post",
		async : false,
		success : function(data) {
			rawTreeData = data;
		},
		error : function() {
			toastr.error("获取菜单数据出错，请联系管理员");
		}
	});
	var data = rawTreeData;
	var primaryIdName = 'id';
	var parentIdName = 'parentMenuId';
	if (!data || data.length == 0 || !primaryIdName || !parentIdName)
		return [];

	var tree = [], rootIds = [], item = data[0], primaryKey = item[primaryIdName], treeObjs = {}, parentId, parent, len = data.length, i = 0;

	while (i < len) {
		item = data[i++];
		primaryKey = item[primaryIdName];
		treeObjs[primaryKey] = item;
		parentId = item[parentIdName];

		if (parentId != -1) {
			parent = treeObjs[parentId];

			if (parent.children) {
				parent.children.push(item);
			} else {
				parent.children = [ item ];
			}
		} else {
			rootIds.push(primaryKey);
		}
	}

	for (var i = 0; i < rootIds.length; i++) {
		tree.push(treeObjs[rootIds[i]]);
	}
	;

	return tree;
}

//排序保存
function updateMenuSortSave() {
	var sorted = $("#sortable").sortable( "serialize");
	console.log(sorted);
	$.ajax({
		url : "../menu/updateSortSave.action",
		data: sorted,
		type : "post",
		success : function(data) {
			if (data.success == "true") {
				subMenuDataReload($("#parentMenuIdHidden").val(), $("#parentMenuLevelHidden").val(), $("#parentMenuName").val());
			}
		},
		error : function() {
			toastr.error("保存新顺序出错，请联系管理员");
		}
	});
}
//子菜单重新加载
function subMenuDataReload(parentMenuId, parentMenuLevel, parentMenuName) {
	$.ajax({
		url: "../menu/query.action",
		data: {"parentMenuId": parentMenuId, "menuLevel": parseInt(parentMenuLevel)+1},
		type: "post",
		success: function (data) {
			var dataReload = '';
			$(data).each(function(){
				dataReload += '<tr style="cursor:move" data-index="0" id="menuId_'+this.id+'" data-uniqueid="'+this.id+'">'
							+		'<td style="">'
							+			parentMenuName
							+		'</td>'
							+		'<td style="">'
							+			this.menuName
							+		'</td>'
							+		'<td style="">'
							+			this.menuOrder
							+		'</td>'
							+	'</tr>';
			});
			$("#sortable").html(dataReload);
			$("#sortable").sortable();
			$("#sortable").disableSelection();
		},
		error: function() {
			toastr.error("刷新数据发生错误，请联系管理员");
		}
	});
}
function returnHome() {
	window.location.href = "../menu/page.action";
}
